package com.lekang.system.service.impl;

import java.util.List;
import java.util.Date;
import com.lekang.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lekang.system.mapper.ContractMapper;
import com.lekang.system.domain.Contract;
import com.lekang.system.service.IContractService;

@Service
public class ContractServiceImpl implements IContractService 
{
    @Autowired
    private ContractMapper contractMapper;

    @Override
    public Contract selectContractById(Long id)
    {
        return contractMapper.selectContractById(id);
    }

    @Override
    public List<Contract> selectContractList(Contract contract)
    {
        return contractMapper.selectContractList(contract);
    }

    @Override
    public int insertContract(Contract contract)
    {
        contract.setCreateTime(DateUtils.getNowDate());
        return contractMapper.insertContract(contract);
    }

    @Override
    public int updateContract(Contract contract)
    {
        return contractMapper.updateContract(contract);
    }

    @Override
    public int deleteContractByIds(Long[] ids)
    {
        return contractMapper.deleteContractByIds(ids);
    }

    @Override
    public int deleteContractById(Long id)
    {
        return contractMapper.deleteContractById(id);
    }

    @Override
    public int updateContractStatus()
    {
        // 查询所有非已失效状态的合同
        List<Contract> activeContracts = contractMapper.selectActiveContracts();
        if (activeContracts.isEmpty()) {
            return 0;
        }

        Date currentTime = new Date();
        int updateCount = 0;

        for (Contract contract : activeContracts) {
            String newStatus = determineContractStatus(contract, currentTime);
            if (!newStatus.equals(contract.getContractStatus())) {
                contract.setContractStatus(newStatus);
                updateCount++;
            }
        }

        // 批量更新状态
        if (updateCount > 0) {
            contractMapper.updateContractStatusBatch(activeContracts);
        }

        return updateCount;
    }

    @Override
    public int terminateContract(Long id, Long submitterId, String terminationAgreement)
    {
        Contract contract = contractMapper.selectContractById(id);
        if (contract == null) {
            return 0;
        }

        contract.setContractStatus("已失效");
        contract.setTerminationSubmitter(submitterId);
        contract.setTerminationTime(new Date());
        contract.setTerminationAgreement(terminationAgreement);

        return contractMapper.updateContract(contract);
    }

    /**
     * 根据合同时间和当前时间判断合同状态
     */
    private String determineContractStatus(Contract contract, Date currentTime) {
        Date startDate = contract.getStartDate();
        Date endDate = contract.getEndDate();

        if (startDate == null || endDate == null) {
            return contract.getContractStatus(); // 保持原状态
        }

        // 当前时间 < 合同开始时间 -> 未生效
        if (currentTime.before(startDate)) {
            return "未生效";
        }
        // 合同结束时间 >= 当前时间 >= 合同开始时间 -> 生效中
        else if (!currentTime.after(endDate)) {
            return "生效中";
        }
        // 当前时间 > 合同结束时间 -> 已过期
        else {
            return "已过期";
        }
    }
}
